<template>
  <div class="dept-container">
    <el-card>
      <!-- 操作按钮 -->
      <el-button type="primary" @click="openAddDialog">新增部门</el-button>

      <!-- 表格 -->
      <el-table :data="deptList" border style="width: 100%; margin-top: 20px">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="name" label="部门名称"></el-table-column>
        <el-table-column prop="parentId" label="父级部门ID"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column prop="createdAt" label="创建时间"></el-table-column>
        <el-table-column prop="updatedAt" label="更新时间"></el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button size="small" @click="openEditDialog(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteDept(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 弹窗：新增 / 编辑 -->
    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑部门' : '新增部门'">
      <el-form ref="formRef" :model="form" label-width="100px">
        <el-form-item label="部门名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="父级部门">
          <el-select v-model="form.parentId" placeholder="请选择父级部门" style="width: 100%">
            <el-option v-for="dept in allDepts" :key="dept.id" :label="dept.name" :value="dept.id" />

          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import request from '@/utils/request';
import { ElMessage } from 'element-plus';
const deptList = ref([]);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);
const allDepts = ref([]);
const dialogVisible = ref(false);
const isEdit = ref(false);
const form = ref({
  id: null,
  name: '',
  parentId: 0,
  description: ''
});
const formRef = ref();

// 加载部门列表
const loadDeptList = () => {
  request.get('/dept/list', {
    params: {
      pageNum: pageNum.value,
      pageSize: pageSize.value
    }
  }).then(res => {
    deptList.value = res.data.records;
    total.value = res.data.total;
    allDepts.value = res.data.records
  });
};

// 打开新增弹窗
const openAddDialog = () => {
  isEdit.value = false;
  form.value = {
    id: null,
    name: '',
    parentId: 0,
    description: ''
  };
  dialogVisible.value = true;
};

// 打开编辑弹窗
const openEditDialog = (row) => {
  isEdit.value = true;
  form.value = { ...row };
  dialogVisible.value = true;
};

// 提交表单（新增或编辑）
const submitForm = () => {
  const url = isEdit.value ? '/dept' : '/dept';
  const method = isEdit.value ? 'put' : 'post';

  request({
    url,
    method,
    data: form.value
  }).then(() => {
    ElMessage.success('操作成功');
    dialogVisible.value = false;
    loadDeptList();
  });
};

// 删除部门
const deleteDept = (id) => {
  request.delete(`/dept/${id}`).then(() => {
    ElMessage.success('删除成功');
    loadDeptList();
  });
};

onMounted(() => {
  loadDeptList();
});
</script>

<style lang="scss" scoped>
.dept-container {
  padding: 20px;
}
</style>